﻿
Imports std

Namespace internalFunctions
    'parameters, i1, input object
    '            i2, input object
    'output, int, use compare(i1,i2)
    Public Class compare
        Inherits internalFunction

        Public Class parameters
            Public Const i1 As String = "i1"
            Public Const i2 As String = "i2"
        End Class

        Private Shared _aliases As aliases = Nothing
        Private Shared m As iParametersMapping = Nothing

        Shared Sub New()
            initialAliases(_aliases, "[?]")
            initialMapping(m, parameters.i1, parameters.i2)
        End Sub

        Public Sub New()
            Me.New("compare", _aliases)
        End Sub

        Protected Sub New(ByVal name As String, ByVal aliases As aliases)
            MyBase.New(name, m, aliases)
        End Sub

        Protected Shared Function compareImpl(Of T1, T2)(ByVal i1 As T1, ByVal i2 As T2) As Int32
            Return Global.compare(Of T1, T2)(i1, i2)
        End Function

        Protected Overridable Function generateOutput(Of T1, T2)(ByVal i1 As T1, ByVal i2 As T2) As bare_variable
            Return New bare_variable(typeDefination.int, compareImpl(i1, i2))
        End Function

        Protected NotOverridable Overrides Function invoke(ByVal ctx As context, _
                                                           ByRef output As bare_variable) As Boolean
            Dim i1 As bare_variable = Nothing
            Dim i2 As bare_variable = Nothing
            i1 = ctx.findVariable(parameters.i1)
            i2 = ctx.findVariable(parameters.i2)

            If i1 Is Nothing OrElse i2 Is Nothing Then
                raiseError("expect parameter " + parameters.i1 + ", or " + parameters.i2, errorHandle.errorType.user)
                Return False
            Else
                output = generateOutput(i1.value(), i2.value())
                Return True
            End If
        End Function
    End Class

    'parameters, same as compare
    'return, bool, whether i1 < i2
    Public Class less
        Inherits compare

        Private Shared _aliases As aliases = Nothing

        Shared Sub New()
            initialAliases(_aliases, "[")
        End Sub

        Public Sub New()
            MyBase.New("less", _aliases)
        End Sub

        Protected Overrides Function generateOutput(Of T1, T2)(ByVal i1 As T1, ByVal i2 As T2) As bare_variable
            Return New bare_variable(typeDefination.bool, compareImpl(i1, i2) < 0)
        End Function
    End Class

    'parameters, same as compare
    'return, bool, whether i1 = i2
    Public Class equal
        Inherits compare

        Public Const functionName As String = "equal"
        Private Shared _aliases As aliases = Nothing

        Shared Sub New()
            initialAliases(_aliases, "?")
        End Sub

        Public Sub New()
            MyBase.New(functionName, _aliases)
        End Sub

        Protected Overrides Function generateOutput(Of T1, T2)(ByVal i1 As T1, ByVal i2 As T2) As bare_variable
            Return New bare_variable(typeDefination.bool, compareImpl(i1, i2) = 0)
        End Function
    End Class
End Namespace